let url = "http://192.168.1.32:8000/api/",
    loginOut = "http://192.168.1.32:8000/",
    loginUrl = "/login?pre=" + location.pathname;

// var url = "http://b.cc/";

//$.ajaxSettings = $.extend($.ajaxSettings, {
//    headers: {'platform': 'web'},
//    xhrFields: {
//        withCredentials: true
//    },
//    crossDomain: true
//});
function getCookie(cname) {
    let name = cname + "=";
    let ca = document.cookie.split(';');
    for (let i = 0; i < ca.length; i++) {
        let c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1);
        if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
    }
    return "";
}

// 提示信息
function tip(msg) {
    layer.open({
        content: msg
        , skin: 'msg'
        , anim: 'scale'
        , time: 2
    })
}

function gotoLogin(open = true) {
    open ? top.location.href = loginUrl : top.open(loginUrl);
}

function post(url, data) {
    Vue.http.interceptors.push((request, next) => {
        // continue to next interceptor
        next((response) => {//在响应之后传给then之前对response进行修改和逻辑判断。对于token时候已过期的判断，就添加在此处，页面中任何一次http请求都会先调用此处方法
            if (response.status === 401) {
                gotoLogin();
            }
            if (response.status == 403) {
                alert('该用户已被禁用');
                gotoLogin();
            }
            return response;

        });
    });
    return Vue.http.post(url, data, {
        credentials: true,
        headers: {
//            'platform': 'web',
            'X-XSRF-TOKEN': decodeURIComponent(getCookie('XSRF-TOKEN'))
        },
        emulateJSON: true,
    })
}

function get(url, data) {
    Vue.http.interceptors.push((request, next) => {
        // continue to next interceptor
        next((response) => {//在响应之后传给then之前对response进行修改和逻辑判断。对于token时候已过期的判断，就添加在此处，页面中任何一次http请求都会先调用此处方法
            if (response.status === 401) {
                gotoLogin();
                return false;
            }
            if (response.status == 403) {
               var ii = window.alert('该用户已被禁用');
               location.href = './login.html'
                return false;
            }
            return response;

        });
    });

    return Vue.http.get(url, data, {
        credentials: true,
        headers: {
//            'platform': 'web',
            'X-XSRF-TOKEN': decodeURIComponent(getCookie('XSRF-TOKEN'))
        },
        emulateJSON: true,
    })
}

function logout(){
    get( 'http://192.168.1.32:8000/logout', {}).then(
        function (res) {
            console.log(res)
            if (res.body.code == 101) {
                tip(res.body.msg);
                location.href = './login.html'
            } else if (res.body.code == 400) {
                tip(res.body.msg);
            } else {
                tip(res.body.msg);
                location.href = './home.html'
            }
        }, function (reason) {
            tip('请求错误')
        });
}

function loading() {
    layer.open({type: 2, shadeClose: false});
}

function layerClose() {
    layer.closeAll();
}

// 获取get请求参数
function getRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

function formatDateTime(timeStamp) {
    timeStamp = parseInt(timeStamp);
    if (timeStamp <= 0) {
        return '';
    }
    var date = new Date();
    date.setTime(timeStamp * 1000);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
}

// 返回
function back() {
    try {
        bsie.close()
    } catch (error) {
        window.history.go(-1)
    }
}

function go() {
    window.history.go(-1)
}